// pages/my/my.js

const axios = require('../../utils/http.js');
const utils = require('../../utils/util.js');
const baseFn = utils.baseFn;
const formatTime = utils.formatTime;
const app = getApp()
var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置
Page({

  /**
   * 页面的初始数据
   */
  data: {
    switchcolor: app.data.switchcolor,
    user_id: app.globalData.userInfo.user_id,
    tabs: [{ id: 1, name: '我发布的' }, { id: 0, name:'我接收的'}],
    activeId: 1,
    sliderOffset: 0,
    sliderLeft: 0,
    checked:false,
    list: [],
    page: 1,
    limit: 10,
    pageMax: false
  },
  getList:function(type=2){
    var _upData={
      user_id:this.data.user_id,
      status: this.data.activeId,
      share_id: this.data.checked,
      page: this.data.page,
      limit: this.data.limit
    }
    axios.POST(true, axios.urlR.myRelease, _upData, (res) => {
      wx.stopPullDownRefresh()
      var _list = res
      this.setData({
        pageMax: (res.length < this.data.limit)
      })
      if (type == 1) {
        _list = this.data.list.concat(res)
        this.setData({
          page:(this.data.page+1),
          list: _list
        })
      } else if (type == 3){
        this.setData({ list: _list, page: 1 })
      }else{
        this.setData({ list: _list})
      }
      
    },()=>{
      this.setData({ list: [] })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: (res) => {
        that.setData({
          sliderLeft: (res.windowWidth / this.data.tabs.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / this.data.tabs.length * this.data.activeIndex
        });
      }
    });
    if (baseFn.notNull(app.globalData.userInfo.user_id)){
      this.setData({
        user_id: app.globalData.userInfo.user_id
      })
    }else{
      //获取user_id
      wx.login({
        success: res => {
          axios.POST(true, axios.urlR.logCode, { code: res.code }, (userRes) => {
            this.setData({
              user_id: userRes.user_id
            })
            this.getList()
          }, () => { })
        }
      })
    }
    if (baseFn.notNull(this.data.user_id)){
      this.getList()
    }
    this.sharePath(options);
  },
  // 切换
  tabClick: function (e) {
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeId: e.currentTarget.dataset.idx,
      pageMax:false,
      page:1
    });
    this.getList(3)
    
  },
  //只显示转发按钮的操作
  switch1Change: function (e) {
    this.setData({ checked: e.detail.value, page: 1})
    this.getList(3)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      sliderOffset: 0,
      checked:false,
      page:1,
      activeId: 1
    })
    wx.startPullDownRefresh({})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    if (baseFn.notNull(this.data.user_id)){
      this.setData({page: 1})
      this.getList()
    }
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(!this.data.pageMax){
      this.getList(1)
    }
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  navigationTo:function(ev){
    var proId = ev.currentTarget.dataset.proid;
    var url = ''
    if (this.data.activeId == 0) {
      url= '../Transmit2/Transmit2?proId=' + proId
    } else {
      url= '../ProjectDetails/ProjectDetails?id=' + proId
    }

    wx.navigateTo({
      url: url,
    })
  },

  //跳到分享过来的页面
  sharePath:function(options){
    var sharePath = options.sharePath;
    var proId = options.proId;
    var Text = options.text;
    var sourceId = options.sourceId;
    var userId = options.userId;
    console.log(userId);
    if(sharePath != null){
      wx.navigateTo({
        url: sharePath + "?proId=" + proId + "&text=" + Text + "&sourceId=" + sourceId + "&shareUserId=" + userId + "&isShare=1"
      })
    }
  },
})
